import { Module } from "@nestjs/common";
import { MulterModule } from "@nestjs/platform-express";
import { diskStorage } from "multer";
import * as path from "path";

import { FileService } from "./file.service";
import { FileController } from "./file.controller";
@Module({
  imports: [
    // MulterModule.register({
    //   dest: "./uploads", // 从根目录开始 而不是 src
    // }),
    MulterModule.register({
      storage: diskStorage({
        // 指定文件存储目录
        destination: path.join("./public/uploads"), // 删了这个文件夹需要自己创建目录或者重启项目由 main.ts 代码创建,否则会报错 error [exceptionshandler] enoent: no such file or directory
        // 通过时间戳来重命名上传的文件名
        filename: (_, file, callback) => {
          const fileName = `${new Date().getTime() + path.extname(file.originalname)}`;
          return callback(null, fileName);
        },
      }),
    }),
  ],
  controllers: [FileController],
  providers: [FileService],
})
export class FileModule {}
